package com.iqiyi.pps.epg.core.service.sginal;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.sginal.TServerCluster;
import com.iqiyi.pps.epg.api.model.web.sginal.TServerClusterDataPage;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.record.RecordChannelSignalDao;
import com.iqiyi.pps.epg.core.dao.sginal.ServerClusterDataDao;
import com.iqiyi.pps.epg.core.dao.sginal.ServerDataDao;
import com.iqiyi.pps.epg.core.model.sginal.Server;
import com.iqiyi.pps.epg.core.model.sginal.ServerCluster;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yanchao_sx on 2018/4/4.
 */
@Service
@Transactional
public class ServerClusterServiceImpl implements ServerClusterService{

    @Autowired
    private ServerClusterDataDao serverClusterDao;
    @Autowired
    private ServerDataDao serverDataDao;
    @Autowired
    private RecordChannelSignalDao recordChannelSignalDao;

    @Override
    public ServerCluster get(int id) {
        return serverClusterDao.get(id);
    }

    @Override
    public int add(ServerCluster entity) {
        serverClusterDao.saveOnly(entity);
        return entity.getId();
    }

    @Override
    public void update(ServerCluster entity) {
        serverClusterDao.save(entity);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public TServerClusterDataPage getServerClusterData(TPage page, TFilter filter) {
        Map<String, String> map = filter.getFilters();
        String tempPattern = "${PARAMS}";
        String totalPattern = "select COUNT(*)";
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        String hql = tempPattern + " from com.iqiyi.pps.epg.core.model.sginal.ServerCluster";
        Page<ServerCluster> dbPage = TransUtil.buildPage(page);

        hql = TransUtil.buildQueryStringByPropertyFilter(dbPage, hql, filterList);

        if (dbPage.isAutoCount()) {
            String totalCount = serverClusterDao.getSession().createQuery(hql.replace(tempPattern, totalPattern)).uniqueResult().toString();
            page.setTotalCount(Long.valueOf(totalCount));
        }

        List result = serverClusterDao.getSession().createQuery(hql.replace(tempPattern, ""))
                .setMaxResults(dbPage.getPageSize())
                .setFirstResult((dbPage.getPageNo() - 1) * dbPage.getPageSize())
                .list();
        TServerClusterDataPage tServerClusterDataPage = new TServerClusterDataPage();
        tServerClusterDataPage.setTPage(page);
        List<TServerCluster> tServerClusterDatas = new ArrayList<TServerCluster>();

        if(result.size()>0){
            for(Object obj : result) {
                ServerCluster serverCluster = (ServerCluster) obj;
                int id = ((ServerCluster) obj).getId();
                int including = 0;
                int maxIncluding = 0;

                hql = "from Server where status !=-1 and serverType =" + Server.STATUS_INCLUDE + " and cluster = "+id;
                List<Server> list = serverDataDao.getSession().createQuery(hql).list();
                for(Server server:list){
                    maxIncluding+=server.getIncluding();
                    including+=recordChannelSignalDao.getServerIpSumByServerIp(server.getInsideServerIp());
                }
                String inc = including +"/"+maxIncluding;
                serverCluster.setIncluding(inc);
                serverCluster.setServerNum(list.size());
                tServerClusterDatas.add(serverCluster.convertThriftModel());
            }
        }
        tServerClusterDataPage.setResult(tServerClusterDatas);
        return tServerClusterDataPage;
    }

    @Override
    public List<ServerCluster> getAll() {
        return serverClusterDao.find();
    }

    @Override
    public List<Server> getAllServerById(int id) {
        Criteria criteria = serverDataDao.createCriteria();
        criteria.add(Restrictions.eq("cluster", id));
        criteria.add(Restrictions.eq("serverType", Server.STATUS_INCLUDE));
        criteria.add(Restrictions.ne("status",Server.STATUS_IS_DELETE));
        List<Server> servers = serverDataDao.find(criteria);
        return servers;
    }

    @Override
    public void delServerCluster(int id) {
        serverClusterDao.delete(id);
    }

    @Override
    public int includingTaskSum(int id) {
        List<Server> list = getAllServerById(id);
        int sum = 0;
        if(list!=null && list.size() > 0){
            for(Server server: list){
                sum+=recordChannelSignalDao.getServerIpSumByServerIp(server.getInsideServerIp());
            }
        }
        return sum;
    }
}
